//
//  FindPasswordViewController.m
//  JingXingApp
//
//  Created by 王亮 on 15/8/8.
//  Copyright (c) 2015年 王亮. All rights reserved.
//

#import "FindPasswordViewController.h"
#import "FindPasswordNextViewController.h"
#import "ChangePasswordNextViewController.h"

@interface FindPasswordViewController ()<UITextFieldDelegate>
{
    int allNum;
}
@property(nonatomic,strong)UITextField *telTextField;
@property(nonatomic,strong)UITextField *codeTextField;
@property(nonatomic,strong)UIButton *repetBtn;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation FindPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    allNum = 60;
    [self UI];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)UI {
    
    self.view.backgroundColor = Color_Back_Gray;
    [self.rightBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    CGFloat rate = 90.0f / 615.0f;
    CGFloat height = CGRectGetWidth(self.view.frame) * rate;
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 12, 100, height)];
    [self.view addSubview:textField1];
    textField1.placeholder = @"输入手机号";
    [self addBackView:textField1];
    textField1.keyboardType = UIKeyboardTypeNumberPad;
    textField1.delegate = self;
    self.telTextField = textField1;
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, CGRectGetHeight(textField1.frame))];
    [self addRightView:rightView];
    textField1.rightView = rightView;
    textField1.rightViewMode = UITextFieldViewModeAlways;
    
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(textField1.frame) + 12, 100, height)];
    [self.view addSubview:textField2];
    [self addBackView:textField2];
    textField2.placeholder = @"输入验证码";
    self.codeTextField = textField2;
    
    UIImage *image = [UIImage imageNamed:@"找回密码_03.png"];
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(textField2.frame) + 69.0f / 2, image.size.width / 2, image.size.height / 2)];
    [self.view addSubview:nextBtn];
    nextBtn.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, nextBtn.center.y);
    [nextBtn setBackgroundImage:image forState:UIControlStateNormal];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)addBackView:(UITextField *)textField {
    
    CGRect frame = textField.frame;
    frame.size.width = CGRectGetWidth(self.view.frame) - 30;
    frame.origin.x = 15;
    textField.frame = frame;
    textField.backgroundColor = [UIColor whiteColor];
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textField.layer.borderWidth = 0.5;
    textField.font = [UIFont systemFontOfSize:15];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 43.0f / 2, CGRectGetHeight(textField.frame))];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}

- (void)addRightView:(UIView *)rightView {
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.5, CGRectGetHeight(rightView.frame))];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [rightView addSubview:lineView];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:rightView.bounds];
    [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [rightView addSubview:btn];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.repetBtn = btn;
    
}

- (void)btnAction:(UIButton *)sender {
    
    NSLog(@"验证码");
    

    BOOL isTel = [BCBaseObject isMobileNumber:self.telTextField.text];
    if (isTel) {
        
        self.repetBtn.userInteractionEnabled = NO;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"user_name"] = self.telTextField.text;
        dic[@"type"] = @"1";
        [WLHttpRequestAfter userSendRangCodeWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
            if ([resultDictionary[@"status"] intValue] == 1) {
                
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"验证码已发送到手机"];
                self.codeTextField.text = [NSString stringWithFormat:@"%@",resultDictionary[@"data"][@"code"]];
            }
            
        } andFailureBlock:^(NSError *resultError) {
            
        }];
    }else {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"电话号码不正确"];
    }
    
}

- (void)nextBtnAction:(UIButton *)sender {
    
    if (![self.codeTextField.text isEqual:@""]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"code"] = self.codeTextField.text;
        dic[@"phone"] = self.telTextField.text;
        [WLHttpRequestAfter userFindPasswordWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
            if ([resultDictionary[@"status"] intValue] == 1) {
        
                self.repetBtn.userInteractionEnabled = YES;
                [self.repetBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                [self.repetBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                [self.timer invalidate];
                self.timer = nil;
                allNum = 60;
                
                if ([self.title isEqual:@"修改密码"]) {
                    
                    ChangePasswordNextViewController *findPasswordNextVC = [[ChangePasswordNextViewController alloc] init];
                    findPasswordNextVC.title = @"修改密码";
                    [self.navigationController pushViewController:findPasswordNextVC animated:YES];
                }else {
                    
                    FindPasswordNextViewController *findPasswordNextVC = [[FindPasswordNextViewController alloc] init];
                    findPasswordNextVC.title = @"找回密码";
                    [self.navigationController pushViewController:findPasswordNextVC animated:YES];
                }
                
            }else if ([resultDictionary[@"status"] intValue] == 0){
                
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"用户不存在"];
            }
            
        } andFailureBlock:^(NSError *resultError) {
            
        }];
    }else {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"请输入验证码"];
    }
    
    
   
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField.text.length < 11 || [string isEqual:@""]) {
        
        return YES;
    }else {
        
        return NO;
    }
}

- (void)timeAction {
    
    allNum --;
    if (allNum < 0) {
        
        self.repetBtn.userInteractionEnabled = YES;
        [self.repetBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.repetBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
        allNum = 60;
        return;
    }
    NSString *nameStr = [NSString stringWithFormat:@"已发送(%d)",allNum];
    [self.repetBtn setTitle:nameStr forState:UIControlStateNormal];
    [self.repetBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}

@end
